package com.taijihuabao.modules.controller;

import com.taijihuabao.common.approveprocess.ApproveProcessFactory;
import com.taijihuabao.common.commonutils.PageResult;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.model.approval.dto.*;
import com.taijihuabao.modules.model.approval.vo.ApprovalVO;
import com.taijihuabao.modules.service.ApprovalProcessService;
import com.taijihuabao.modules.service.ApprovalService;
import com.taijihuabao.modules.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaoboyu
 */
@Api(tags = "审批")
@RestController
@RequestMapping("/api/v1/approveProcess")
public class ApprovalProcessController {

    @Autowired
    private ApproveProcessFactory approveProcessFactory;
    @Resource
    private ApprovalService approvalService;
    @Resource
    private FileService fileService;

    /**
     * 部署流程图
     *
     * @param processNameDTO 流程名称DTO
     * @return
     */
    @ApiOperation("部署流程图")
    @GetMapping("/deploy")
    public JsonResult deploy(ProcessNameDTO processNameDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(processNameDTO.getProcessName());
        approvalProcessService.deploy();
        return JsonResult.success();
    }

    /**
     * 文件提交
     *
     * @param file
     * @return
     */
    @ApiOperation("上传请假证明")
    @PostMapping("/upload")
    public JsonResult uploadProve(MultipartFile file) {
        String proveUrl = fileService.uploadProve(file);
        return JsonResult.success(proveUrl);
    }

    /**
     * 启动流程
     *
     * @param approvalProcessDTO 流程参数DTO
     * @return
     */
    @ApiOperation("启动流程")
    @PostMapping("/start")
    public JsonResult insertApproval(@RequestBody ApprovalProcessDTO approvalProcessDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(approvalProcessDTO.getProcessName());
        String approvalCode = approvalProcessService.startProcess(approvalProcessDTO);
        return JsonResult.success(approvalCode);
    }

    /**
     * 查询代办
     *
     * @param processNameDTO 流程名称DTO
     * @return 登录用户待办任务
     */
    @ApiOperation("查询代办")
    @GetMapping("/findTask")
    public JsonResult findTask(ProcessNameDTO processNameDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(processNameDTO.getProcessName());
        List<ApprovalVO> task = approvalProcessService.findTask(processNameDTO.getProcessName());
        return JsonResult.success(task);
    }

    /**
     * @param processNameDTO 流程名称DTO
     * @return 登录用户已办任务
     */
    @ApiOperation("查询已办")
    @GetMapping("/findCompleteTask")
    public JsonResult findCompleteTask(ProcessNameDTO processNameDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(processNameDTO.getProcessName());
        List<ApprovalVO> completeTask = approvalProcessService.findCompleteTask(processNameDTO.getProcessName());
        return JsonResult.success(completeTask);
    }

    /**
     * 查询流程进度
     *
     * @param findActivitiesDTO 审批编码，流程名称
     * @return
     */
    @ApiOperation("查询流程进度")
    @PostMapping("/findActivitis")
    public JsonResult findActivities(@RequestBody FindActivitiesDTO findActivitiesDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(findActivitiesDTO.getProcessName());
        List<ApprovalVO> activitis = approvalProcessService.findActivities(findActivitiesDTO.getApprovalCode());
        return JsonResult.success(activitis);
    }

    /**
     * 重新提交申请
     *
     * @param approvalProcessDTO
     * @return
     */
    @ApiOperation("重新提交申请")
    @PutMapping("/reSubmit")
    public JsonResult reSubmit(@RequestBody ApprovalProcessDTO approvalProcessDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(approvalProcessDTO.getProcessName());
        String approvalCode = approvalProcessService.reSubmit(approvalProcessDTO);
        return JsonResult.success(approvalCode);
    }

    /**
     * 审批任务
     *
     * @param approvalProcessDTO
     * @return
     */
    @ApiOperation("完成待审批")
    @PostMapping("/completeTask")
    public JsonResult completeTask(@RequestBody ApproveCompleteDTO approvalProcessDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(approvalProcessDTO.getProcessName());
        approvalProcessService.completeTask(approvalProcessDTO);
        return JsonResult.success();
    }

    /**
     * 根据审批编码查询单个审批单
     *
     * @param approvalCodeDTO 审批编码DTO
     * @return
     */
    @PostMapping("/approvalCode")
    @ApiOperation("根据审批编码查询单个审批单")
    public JsonResult findApproval(@RequestBody ApprovalCodeDTO approvalCodeDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(approvalCodeDTO.getProcessName());
        return JsonResult.success(approvalProcessService.selectByApprovalCode(approvalCodeDTO.getApprovalCode()));
    }

    /**
     * 根据审批编码查询单个审批单
     *
     * @param byApplyUserDTO
     * @return
     */
    @PostMapping("/ApplyUser")
    @ApiOperation("查询申请人的所有审批单")
    public JsonResult selectByApplyUserId(@RequestBody ByApplyUserDTO byApplyUserDTO) {
        ApprovalProcessService approvalProcessService =
                approveProcessFactory.getApprovalProcessServiceImpl(byApplyUserDTO.getProcessName());
        PageResult pageResult = approvalProcessService
                .selectByApplyUserId(byApplyUserDTO.getPage(), byApplyUserDTO.getSize());
        return JsonResult.success(pageResult);
    }

    /**
     * 加签
     *
     * @param endorseDTO
     * @return
     */
    @PostMapping("/addAssginee")
    @ApiOperation("加签")
    public JsonResult addTaskAssginees(@RequestBody EndorseDTO endorseDTO) {
        approvalService.addTaskAssingnees(endorseDTO);
        return JsonResult.success();
    }

}
